<template>
  <div class="resumeItemContainer">
    <div class="resumeItemTitle">
      <div class="resumeItemTitleFlag"></div>
      <span class="resumeItemTitleContent">项目经验</span>
      <span class="resumeItemTitleAddIcon" @click="handleAddClick"><img :src="resumeItemAddIcon"
          class="resumeItemAddIcon"> 添加</span>
    </div>
    <div class="resumeItemMain">
      <div class="resumeItemDisplayList">
        <div class="resumeItemDisplayItem" v-for="(item, index) in resumeDataList" :key="index">
          <div class="resumeItemDisplayItemEditButtons">
            <template v-if="!item.editStatus">
              <span class="resumeItemDisplayItemEditButton" @click="changeEditStatus(item, true)">
                <img :src="myResumeEditIcon" /> 编辑</span>
              <span class="resumeItemDisplayItemEditButton" @click="handleDeleteClick(item)">
                <img :src="myResumeDeleteIcon" />删除</span>
            </template>
          </div>
          <div class="displayBlock" v-if="!item.editStatus">
            <!-- 非编辑状态 -->
            <div class="displayInfo">
              <span class="bold"> {{ item.person_projectName }} </span>
              <span class="divide" v-if="item.jtzw"></span>
              <span class="bold"> {{ item.jtzw }}</span>
              <span class="divide" v-if="
                  item.person_projectstartdatev2 &&
                  item.person_projectstopdatev2
                "></span>
              <span>
                {{ item.person_projectstartdatev2 }} -
                {{ item.person_projectstopdatev2 }}</span>
            </div>
            <div class="summaryContainer">
              <div class="summary richContent" :ref="`richContent${index}`" v-html="item.person_projectDesc"></div>
              <div class="expendContainer" @click="expandContent(item,index)" v-if="item.showExpend">
                <el-button type="text" :icon="item.expendButtonText=='展开'?'el-icon-arrow-down':'el-icon-arrow-up'">
                  {{item.expendButtonText||"展开"}}
                </el-button>
              </div>
            </div>
          </div>
          <div class="editBlock" v-else :id="item.editStatus?'edittingBlock':''">
            <!-- 编辑状态 -->
            <div class="resumeItemEdit">
              <el-form :ref="'ruleForm' + item.person_projectId" :rules="rules" :model="item.editForm">
                <el-form-item label="项目名称:" prop="person_projectName">
                  <el-input v-model="item.editForm.person_projectName" placeholder="请输入项目名称" style="width: 250px"
                    maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="项目时间:" prop="startdate">
                  <el-date-picker style="width: 250px; height: 40px" v-model="item.editForm.startdate" size="small"
                    type="monthrange" align="right" unlink-panels value-format="yyyy-MM-dd" range-separator="至"
                    start-placeholder="开始时间" end-placeholder="结束时间">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="项目经验内容:" prop="person_projectDesc" class="textAreaFormItem">
                  <el-input type="textarea" :rows="10" placeholder="请输入项目经验内容"
                    v-model="item.editForm.person_projectDesc" :maxlength="1600" show-word-limit>
                  </el-input>
                </el-form-item>
                <el-form-item class="wholeLineFormItem">
                  <div class="submitButtons">
                    <el-button size="small" @click="handleCancelClick(item)">取 消</el-button>
                    <el-button size="small" type="primary" @click="handleSave(item)">完 成</el-button>
                  </div>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import myResumeDeleteIcon from "@/assets/images/png/myResumeDeleteIcon.png";
import myResumeEditIcon from "@/assets/images/png/myResumeEditIcon.png";
import myResumeItemsMixin from "@/mixin/myResumeItemsMixin.js";
export default {
  name: "projectInfo",
  mixins: [myResumeItemsMixin],
  props: {
    resumeData: {
      type: Array,
      default: () => [],
    },
    editStatus: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      myResumeEditIcon,
      myResumeDeleteIcon,
      resumeDataList: [], //
      rules: {
        person_projectName: [
          { required: true, message: "请输入项目名称", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        person_projectDesc: [
          { required: true, message: "请输入项目经验内容", trigger: "blur" },
          {
            min: 2,
            max: 1600,
            message: "长度在 2 到 1600 个字符",
            trigger: "blur",
          },
        ],
        startdate: [
          { required: true, message: "请选择项目时间", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    resumeData: {
      immediate: true,
      handler(newValue, oldValue) {
        //console.log("项目经历projectInfo基本信息更新：", newValue);
        if (!newValue) return;
        this.resumeDataList = newValue.map((item, index) => {
          if (!item.hasOwnProperty("editStatus")) {
            this.$set(item, "editStatus", false);
          }
          if (!item.hasOwnProperty("editForm")) {
            if (item.person_projectstopdatev2 == "至今") {
              //如果是至今就取这个月的一号，
              item.person_projectstopdatev2 = new Date()
                .toLocaleDateString()
                .replace(/\//g, "-");
            }
            this.$set(item, "editForm", {
              //这个数组中的时间单个元素是yyyy-MM-dd的格式
              startdate: [
                item.person_projectstartdate,
                item.person_projectstopdate ? item.person_projectstopdate : "",
              ],
              person_projectName: item.person_projectName,
              person_projectDesc: item.person_projectDesc,
              person_projectId: item.person_projectId,
            });
            // console.log("item.editForm:", item.editForm);
          }

          if (!item.hasOwnProperty("showExpend")) {
            this.isOverflowing(item, `richContent${index}`);
          }
          return item;
        });
      },
    },
    editStatus: {
      handler(newValue, oldValue) {
        //如果新值是false要把所有的editStatus改为false
        if (newValue == false) {
          this.resumeDataList.forEach((item) => {
            item.editStatus = false;
            if (item.isNew) {
              this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1); // 新的删除
            }
          });
        }
      },
    },
  },
  methods: {
    handleDeleteClick(item) {
      this.$confirm("确定删除该条项目经验吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //下面才是删除方法
          const finalParams = {
            op: "cma_weixin_deal",
            func: "updataObjColumn",
            // version: "7.3.5",
            forName: "iphone",
            param: {
              person_id: this.personId,
              conditionArr: {
                delproject: item.person_projectId,
              },
            },
          };
          //console.log("handleDeleteClick-----------finalParams:", finalParams);
          this.resumeGeneralInterface(
            finalParams,
            () => {
              this.handleCancelClick(item);
              this.$message.success("操作成功");
              this.$emit("reqGetIntenedInfo");
            },
            () => {}
          );
        })
        .catch(() => {});
    },
    handleCancelClick(item) {
      if (item.isNew) {
        this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1);
      } else {
        //初始化赋值
        this.$set(item, "editStatus", false);
        this.$set(item, "editForm", {
          startdate: [
            item.person_projectstartdate,
            item.person_projectstopdate,
          ],
          person_projectName: item.person_projectName,
          person_projectDesc: item.person_projectDesc,
          person_projectId: item.person_projectId,
        });
      }
      this.changeEditStatus(item, false);
    },
    async handleSave(item) {
      this.$refs["ruleForm" + item.editForm.person_projectId][0].validate(
        (valid) => {
          if (valid) {
            var finalParams = {};
            if (item.isNew) {
              //添加
              finalParams = {
                op: "person_sub_busi",
                func: "addPersonProject",
                version: "v5.9.18",
                forName: "iphone",
                param: {
                  person_id: this.personId,
                  insertArr: {
                    person_projectstartdate: item.editForm.startdate[0],
                    person_projectstopdate: item.editForm.startdate[1],
                    person_projectName: item.editForm.person_projectName,
                    person_projectDesc: item.editForm.person_projectDesc,
                  },
                },
              };
            } else {
              //修改
              finalParams = {
                op: "person_sub_busi",
                func: "updatePersonProject",
                version: "v5.9.18",
                forName: "iphone",
                param: {
                  person_id: this.personId,
                  updateArr: {
                    person_projectstartdate: item.editForm.startdate[0],
                    person_projectstopdate: item.editForm.startdate[1],
                    person_projectName: item.editForm.person_projectName,
                    person_projectDesc: item.editForm.person_projectDesc,
                    person_projectId: item.person_projectId,
                  },
                },
              };
            }
            //console.log("handleSave-----------finalParams:", finalParams);
            this.resumeGeneralInterface(
              finalParams,
              () => {
                this.handleCancelClick(item);
                this.$message.success("操作成功");
                this.$emit("reqGetIntenedInfo");
              },
              () => {}
            );
          } else {
            //console.log("error submit!!");
            return false;
          }
        }
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.resumeItemContainer {
  display: flex;
  flex-direction: column;
  margin: 20px 0 16px;
  .resumeItemTitle {
    display: flex;
    align-items: center;
    .resumeItemTitleFlag {
      width: 4px;
      height: 19px;
      background: #0b6eff;
      border-radius: 2px;
      margin-right: 10px;
    }
    .resumeItemTitleContent {
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 20px;
      color: #333333;
      margin-right: 19px;
    }
    .resumeItemTitleAddIcon {
      display: flex;
      align-items: center;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #4880ff;
      cursor: pointer;
      .resumeItemAddIcon {
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
    }
  }
  .resumeItemMain {
    margin-top: 16px;
    .resumeItemDisplayList {
      .resumeItemDisplayItem {
        position: relative;
        margin-bottom: 16px;
        &:last-of-type {
          margin-bottom: 0px;
        }
        &:first-of-type {
          .displayBlock {
            .summaryContainer {
              .summary {
                margin-top: 0px;
              }
            }
          }
        }
        &:hover {
          .displayBlock {
            background: #f8f8f8;
            border-radius: 8px;
            padding: 16px 16px 16px 16px;
          }
          .resumeItemDisplayItemEditButtons {
            display: block;
          }
        }
        .resumeItemDisplayItemEditButtons {
          display: none;
          position: absolute;
          top: -18px;
          right: 0;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          color: #4880ff;
          line-height: 18px;
          & > span {
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            margin-left: 23px;
            user-select: none;
            & > img {
              width: 16px;
              height: 16px;
              margin-right: 5px;
            }
          }
        }
        .displayBlock {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          // margin-right: 150px;
          color: #666666;
          line-height: 18px;
          text-align: left;
          font-style: normal;
          .displayInfo {
            display: flex;
            align-items: center;
          }
          .summary {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 14px;
            color: #888888;
            line-height: 21px;
            text-align: left;
            font-style: normal;
            margin-top: 9px;
            margin-bottom: 0px;
          }
          .bold {
            font-weight: bold;
            font-size: 15px;
            color: #000000;
            line-height: 21px;
          }
          .divide {
            width: 1px;
            height: 11px;
            background: #cccfd7;
            margin: 0 12px;
          }
        }
        .editBlock {
          margin-top: 2px;
          background: #f8f8f8;
          border-radius: 8px;
          padding: 16px;
          ::v-deep .el-form-item {
            display: inline-flex;
            flex-direction: column;
            min-width: 251px;
            width: calc(33% - 40px);
            margin-right: 24px;
            margin-bottom: 16px;
            margin-top: 0px;
            &.textAreaFormItem {
              width: 100%;
              margin-bottom: 14px;
              .el-textarea {
                textarea {
                }
              }
            }
            &.wholeLineFormItem {
              width: 100%;
              margin-bottom: 0px;
              .el-form-item__content {
                display: flex;
                justify-content: space-between;
                .submitButtons {
                  width: 100%;
                  display: flex;
                  justify-content: flex-end;
                  margin-top: 11px;
                  .el-button {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 100px;
                    height: 30px;
                    background: #ffffff;
                    border-radius: 4px;
                    border: 1px solid #cdcdcd;
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 14px;
                    text-align: left;
                    font-style: normal;
                    padding: 0;
                    &.el-button--primary {
                      width: 100px;
                      height: 30px;
                      background: #4880ff;
                      border-radius: 4px;
                    }
                  }
                }
                &::before {
                  display: none;
                }
                &::after {
                  display: none;
                }
              }
            }
            .el-form-item__label {
              height: 22px;
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 14px;
              color: #666660;
              line-height: 20px;
              text-align: left;
              font-style: normal;
            }
          }
          .submitButtons {
            display: flex;
            justify-content: flex-end;
            .el-button {
              display: flex;
              justify-content: center;
              align-items: center;
              width: 100px;
              height: 30px;
              background: #ffffff;
              border-radius: 4px;
              border: 1px solid #cdcdcd;
              font-family: PingFang-SC, PingFang-SC;
              font-weight: bold;
              font-size: 14px;
              text-align: left;
              font-style: normal;
              padding: 0;
              &.el-button--primary {
                width: 100px;
                height: 30px;
                background: #4880ff;
                border-radius: 4px;
              }
            }
          }
        }
      }
    }
  }
  // ::v-deep .el-date-editor .el-range-input {
  //   background-color: #ffffff;
  // }
  ::v-deep .el-textarea {
    margin-top: 4px;
  }
  ::v-deep .el-select {
    .el-input__inner {
      width: 250px;
    }
  }
}
::v-deep .el-textarea .el-input__count {
  background-color: transparent;
  bottom: -33px;
}
::v-deep .el-range-separator {
  display: inline-flex;
  align-items: center;
}
::v-deep .el-textarea .el-input__count {
  background-color: transparent;
}
</style>
